//
//  WmcSecurityTemplateView.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/4/7.
//

import UIKit

/// 物业水印模板
class WmcSecurityTemplateView: WmcBaseWatermarkTemplateView {
    lazy var iconImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFit
        return imageView
    }()

    lazy var dayTimeLabel: WmcDateLabel = {
        let label = WmcDateLabel.YP.Create(font: WmcFontManager.dinAlternateFont(9),
                                           color: UIColor.white,
                                           alignment: .center)
        label.leftEdge = 0
        label.format = .yyyy_MM_dd
        return label
    }()

    lazy var lineView: UIView = {
        let view = UIView()
        return view
    }()

    override func config() {
        super.config()
        defaultWidth = 225.wmc_autoHor

        backgroundColorView.layer.cornerRadius = 2
        backgroundColorView.layer.masksToBounds = true
        backgroundColorView.backgroundColor = .white
        addSubview(backgroundColorView)

        addSubview(iconImageView)

        titleLabel.font = 18.yp.uiFont(weight: .medium)
        titleLabel.textAlignment = .left
        addSubview(titleLabel)

        timeLabel.font = WmcFontManager.dinAlternateFont(14)
        timeLabel.leftEdge = 0
        timeLabel.format = .HH_mm
        timeLabel.textAlignment = .center
        timeLabel.textColor = .white
        timeLabel.layer.cornerRadius = 2
        timeLabel.layer.masksToBounds = true
        addSubview(timeLabel)

        addSubview(dayTimeLabel)

        addSubview(lineView)

        stackView.axis = .vertical
        stackView.distribution = .equalSpacing
        stackView.alignment = .fill
        stackView.spacing = 0
        addSubview(stackView)
    }

    override func configWatermarkModel(_ watermarkModel: WmcWatermarkDetailModel, styleMode: WmcBaseWatermarkTemplateView.StyleMode) {
        super.configWatermarkModel(watermarkModel, styleMode: styleMode)
        
        timeLabel.styleMode = styleMode
        if let staticDate = watermarkModel.staticData.staticDate, styleMode.isStatic {
            timeLabel.updateDate(staticDate)
        }
        
        let alpha = calculateAlpha(watermarkModel.alpha)
        backgroundColorView.alpha = alpha

        // icon
        iconImageView.kf.setImage(with: watermarkModel.icon.yp.url_optional)

        var drawViews: [UIView] = []
        // 获取标题最大字数
        let maxTitleCount = watermarkModel.fieldTitleMaxCount
        watermarkModel.fields.forEach { option in
            guard option.isOpen else { return }
            switch option.fieldType {
            case .title: // 标题
                titleLabel.text = option.content
                // 标题色和线色图片色统一
                titleLabel.textColor = option.uiTextColor(.wmc_blue_203CC0)
                iconImageView.yp.set(tintColor: titleLabel.textColor)
                timeLabel.backgroundColor = titleLabel.textColor
                dayTimeLabel.textColor = titleLabel.textColor
                lineView.backgroundColor = titleLabel.textColor

            case .time: // 时间
                break // 不做处理

            case .addr: // 地点
                let item = createLocationItem(option: option,
                                              styleMode: styleMode,
                                              staticAddress: watermarkModel.staticData.drawStaticAddress,
                                              color: option.uiTextColor(.wmc_blue_203CC0),
                                              maxTitleCount: maxTitleCount,
                                              locationFormat: .location)
                item.backgroundColor = option.uiBgColor.yp.alpha(alpha)
                drawViews.append(item)

            case .lat: // 经纬
                let item = createLocationItem(option: option,
                                              styleMode: styleMode,
                                              staticAddress: watermarkModel.staticData.drawStaticAddress,
                                              color: option.uiTextColor(.wmc_blue_203CC0),
                                              maxTitleCount: maxTitleCount,
                                              locationFormat: .longitudLatitude)
                item.backgroundColor = option.uiBgColor.yp.alpha(alpha)
                drawViews.append(item)

            case .weather: // 天气
                let item = createWeatherItem(option: option,
                                             styleMode: styleMode,
                                             staticWeater: watermarkModel.staticData.staticWeater,
                                             color: option.uiTextColor(.wmc_blue_203CC0),
                                             maxTitleCount: maxTitleCount,
                                             weatherFormat: .weatherTemperature)
                item.backgroundColor = option.uiBgColor.yp.alpha(alpha)
                drawViews.append(item)

            case .altitude: // 海拔
                let item = createAltitudeItem(option: option,
                                              styleMode: styleMode,
                                              staticLocation: watermarkModel.staticData.staticLocation,
                                              color: option.uiTextColor(.wmc_blue_203CC0),
                                              maxTitleCount: maxTitleCount,
                                              altitudeFormat: .meters)
                item.backgroundColor = option.uiBgColor.yp.alpha(alpha)
                drawViews.append(item)

            case .azimuth: // 方位角
                let item = createAzimuthItem(option: option,
                                             styleMode: styleMode,
                                             staticAzimuth: watermarkModel.staticData.staticAzimuth,
                                             color: option.uiTextColor(.wmc_blue_203CC0),
                                             maxTitleCount: maxTitleCount,
                                             azimuthFormat: .default)
                item.backgroundColor = option.uiBgColor.yp.alpha(alpha)
                drawViews.append(item)

            case .decibe: // 分贝
                let item = createDecibeItem(option: option,
                                            styleMode: styleMode,
                                            color: option.uiTextColor(.wmc_blue_203CC0),
                                            maxTitleCount: maxTitleCount,
                                            decibeFormat: .describeDB)
                item.backgroundColor = option.uiBgColor.yp.alpha(alpha)
                drawViews.append(item)

            case .speed: // 速度
                let item = createSpeedItem(option: option,
                                           styleMode: styleMode,
                                           staticLocation: watermarkModel.staticData.staticLocation,
                                           color: option.uiTextColor(.wmc_blue_203CC0),
                                           maxTitleCount: maxTitleCount,
                                           speedFormat: .km_h)
                item.backgroundColor = option.uiBgColor.yp.alpha(alpha)
                drawViews.append(item)

            case .remark,
                 .record,
                 .text,
                 .custom: // 备注、记工、输入
                let item = createDefaultTextItem(option: option,
                                                 color: option.uiTextColor(.wmc_blue_203CC0),
                                                 maxTitleCount: maxTitleCount)
                item.backgroundColor = option.uiBgColor.yp.alpha(alpha)
                drawViews.append(item)

            case .brand: // 品牌图
                configOptionBrand(option)

            default: break
            }
        }

        // 添加进堆栈视图
        stackView.yp.arrangedSubviews = drawViews
    }

    override func makeConstraints() {
        super.makeConstraints()

        backgroundColorView.snp.makeConstraints { make in
            make.top.left.equalTo(8)
            make.right.equalTo(-8)
            if isShowCertificationWatermark {
                make.bottom.equalTo(certificationView.snp.top).offset(-4)
            } else {
                make.bottom.equalToSuperview().offset(-8)
            }
        }

        iconImageView.snp.makeConstraints { make in
            make.top.equalTo(18)
            make.left.equalTo(15)
            make.size.equalTo(20.wmc_autoHor)
        }

        timeLabel.snp.makeConstraints { make in
            make.top.equalTo(backgroundColorView).offset(7)
            make.right.equalTo(backgroundColorView).offset(-10)
            make.height.equalTo(16)
        }

        dayTimeLabel.snp.makeConstraints { make in
            make.left.right.equalTo(timeLabel)
            make.top.equalTo(timeLabel.snp.bottom).offset(3)
            make.height.equalTo(10)
        }

        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(iconImageView.snp.right).offset(4)
            make.right.equalTo(timeLabel.snp.left).offset(-2)
            make.top.equalTo(timeLabel)
            make.bottom.equalTo(dayTimeLabel)
        }

        lineView.snp.makeConstraints { make in
            make.top.equalTo(dayTimeLabel.snp.bottom).offset(3)
            make.left.equalTo(iconImageView)
            make.right.equalTo(dayTimeLabel)
            make.height.equalTo(2)
        }

        stackView.snp.makeConstraints { make in
            make.top.equalTo(lineView.snp.bottom).offset(8)
            make.left.equalTo(8)
            make.right.equalTo(lineView)
            make.bottom.equalTo(backgroundColorView).offset(-8)
        }
    }
}
